package inc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileKit {

	/**
	 * @param pocess file
	 * @author lewdy
	 */
	private String path;
	private File file;
	
	//constructor
	public FileKit(String filePath) {
		this.path = filePath;
		file = new File(path);
	}
	
	public FileKit(File file){
		this.file = file;
	}
	
	/*
	 * @return size of file : Byte
	 * 1M = 1024 KB, 1KB = 1024 Byte
	 */
	public long size() {
		/*
		if(file.exists()) {
			try {
				return (long)new FileInputStream(file).available();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				System.out.println("FileKit.size: FileNotFound!");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("FileKit.size: IOException!");
				e.printStackTrace();
			}
		}else{
			System.out.println("FileKit.size: FileNotFound!");
		}
		*/
		return this.file.length();
	}
	
	//获取文件扩展�?
	public String getExtension() {
		String fileName = file.getName();
		return fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
	}
	
	//复制文件到destPath
	public  void  copyFileTo(String  destPath)  {  
		String oldPath = this.path;
		String newPath = destPath;
	       try  {  
	           int  bytesum  =  0;  
	           int  byteread  =  0;  
	           File  oldfile  =  new  File(oldPath);  
	           if  (oldfile.exists())  {  //文件存在�? 
	               InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文�? 
	               FileOutputStream  fs  =  new  FileOutputStream(newPath);  
	               byte[]  buffer  =  new  byte[1444];  
	               int  length;  
	               while  (  (byteread  =  inStream.read(buffer))  !=  -1)  {  
	                   bytesum  +=  byteread;  //字节�? 文件大小  
	                   fs.write(buffer,  0,  byteread);  
	               }  
	               inStream.close();  
	           }  
	       }  
	       catch  (Exception  e)  {  
	           System.out.println("复制单个文件操作出错");  
	           e.printStackTrace();  
	       }  
	   } 
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileKit f = new FileKit("E:/RPC/FJ-0856.psd");
		System.out.println(f.size());
	}

}
